import { command } from './command';

/**
 * Types
 */
import type { DeviceManagerType } from '../typings';

/**
 * 通用接口
 *
 * @see https://alidocs.dingtalk.com/i/nodes/dpYLaezmVNDavNvbH1vA4n5n8rMqPxX6
 */
export const common = (dm: DeviceManagerType) => {
    // ==================================================
    /**
     * 定时
     *
     * @see https://alidocs.dingtalk.com/i/nodes/ydxXB52LJqr2G3GAI1QG6vEL8qjMp697
     */
    // ==================================================
    dm.calibrationTime = function (content: any) {
        return command({
            method: 'calibrationTime',
            methodId: 'ppaYygpU',
            params: content,
            to: this.to,
        });
    };

    dm.countdown = function (content: any) {
        // 查找 methodId
        let methodId = '';
        if (this[content.method]) {
            const method = this[content.method].toString();
            const result = method.match(/methodId:\s*'(\w+)'/);
            methodId = result ? result[1] : '';
        }

        return command({
            method: 'countdown',
            methodId: 'pUMNfegC',
            params: {
                ...content,
                methodId,
            },
            to: this.to,
        });
    };

    dm.cancelCountdown = function (content: any) {
        return command({
            method: 'cancelCountdown',
            methodId: 'pgKyRUEL',
            params: content,
            to: this.to,
        });
    };

    dm.getCountdown = function (content: any) {
        return command({
            method: 'getCountdown',
            methodId: 'pZIAX6YA',
            params: content,
            to: this.to,
        });
    };

    dm.getCountdownList = function (content: any) {
        return command({
            method: 'getCountdownList',
            methodId: 'pklFaiux',
            params: content,
            to: this.to,
        });
    };

    dm.pauseCountdown = function (content: any) {
        return command({
            method: 'pauseCountdown',
            methodId: 'pG25pQVr',
            params: content,
            to: this.to,
        });
    };

    dm.resetCountdown = function (content: any) {
        return command({
            method: 'resetCountdown',
            methodId: 'pDXJIw5Z',
            params: content,
            to: this.to,
        });
    };

    dm.resumeCountdown = function (content: any) {
        return command({
            method: 'resumeCountdown',
            methodId: 'p1SEnPAd',
            params: content,
            to: this.to,
        });
    };

    dm.timer = function (content: any) {
        // 查找 methodId
        let methodId = '';
        if (this[content.method]) {
            const method = this[content.method].toString();
            const result = method.match(/methodId:\s*'(\w+)'/);
            methodId = result ? result[1] : '';
        }

        return command({
            method: 'timer',
            methodId: 'phsPkC1S',
            params: {
                ...content,
                methodId,
            },
            to: this.to,
        });
    };

    dm.cancelTimer = function (content: any) {
        return command({
            method: 'cancelTimer',
            methodId: 'pdV8jrX3',
            params: content,
            to: this.to,
        });
    };

    dm.getTimer = function (content: any) {
        return command({
            method: 'getTimer',
            methodId: 'pt8nxAyV',
            params: content,
            to: this.to,
        });
    };

    dm.getTimerList = function (content: any) {
        return command({
            method: 'getTimerList',
            methodId: 'pzuKhH2b',
            params: content,
            to: this.to,
        });
    };

    // ==================================================
    /**
     * 电池电量
     *
     * @see https://alidocs.dingtalk.com/i/nodes/m9bN7RYPWdOdoGobfAKAYdy78Zd1wyK0
     */
    // ==================================================
    dm.getBatteryLevel = function (content: any) {
        return command({
            method: 'getBatteryLevel',
            methodId: 'p0qz6vZq',
            params: content,
            to: this.to,
        });
    };

    dm.getChargingStatus = function (content: any) {
        return command({
            method: 'getChargingStatus',
            methodId: 'pByNzNwk',
            params: content,
            to: this.to,
        });
    };

    dm.getChargingTime = function (content: any) {
        return command({
            method: 'getChargingTime',
            methodId: 'pXFOoTM1',
            params: content,
            to: this.to,
        });
    };

    dm.getDischargingTime = function (content: any) {
        return command({
            method: 'getDischargingTime',
            methodId: 'pj55qJWo',
            params: content,
            to: this.to,
        });
    };

    dm.batteryOnReceive = function (content: any) {
        return command({
            method: 'batteryOnReceive',
            methodId: 'pN4EGobR',
            params: content,
            to: this.to,
        });
    };

    // ==================================================
    /**
     * 网络连接
     *
     * @see https://alidocs.dingtalk.com/i/nodes/YndMj49yWjDgGeGEHxRxqR4pJ3pmz5aA
     */
    // ==================================================
    dm.getDownstreamTransferRate = function (content: any) {
        return command({
            method: 'getDownstreamTransferRate',
            methodId: 'Ug7SsYNG',
            params: content,
            to: this.to,
        });
    };

    dm.getIp = function (content: any) {
        return command({
            method: 'getIp',
            methodId: 'UwYeJE7b',
            params: content,
            to: this.to,
        });
    };

    dm.getLatecy = function (content: any) {
        return command({
            method: 'getLatecy',
            methodId: 'Ua0ReEN9',
            params: content,
            to: this.to,
        });
    };

    dm.getNetworkStatus = function (content: any) {
        return command({
            method: 'getNetworkStatus',
            methodId: 'pWiytWpz',
            params: content,
            to: this.to,
        });
    };

    dm.getNetworkType = function (content: any) {
        return command({
            method: 'getNetworkType',
            methodId: 'pqk1SRfW',
            params: content,
            to: this.to,
        });
    };

    dm.getPacketLossRate = function (content: any) {
        return command({
            method: 'getPacketLossRate',
            methodId: 'UO1x46On',
            params: content,
            to: this.to,
        });
    };

    dm.getMac = function (content: any) {
        return command({
            method: 'getMac',
            methodId: 'UdGDv9Eq',
            params: content,
            to: this.to,
        });
    };

    dm.getRSSI = function (content: any) {
        return command({
            method: 'getRSSI',
            methodId: 'UnCX7zgI',
            params: content,
            to: this.to,
        });
    };

    dm.getTransferRate = function (content: any) {
        return command({
            method: 'getTransferRate',
            methodId: 'UmmnBIYE',
            params: content,
            to: this.to,
        });
    };

    dm.getUpstreamTransferRate = function (content: any) {
        return command({
            method: 'getUpstreamTransferRate',
            methodId: 'UDFq0cY3',
            params: content,
            to: this.to,
        });
    };

    dm.getWifi = function (content: any) {
        return command({
            method: 'getWifi',
            methodId: 'Uhn7cIOx',
            params: content,
            to: this.to,
        });
    };

    // ==================================================
    /**
     * 参数存储
     *
     * @see https://alidocs.dingtalk.com/i/nodes/vy20BglGWOgm9k9Ki19jyda0WA7depqY
     */
    // ==================================================
    dm.getBuildVersion = function (content: any) {
        return command({
            method: 'getBuildVersion',
            methodId: 'UkmrCM31',
            params: content,
            to: this.to,
        });
    };

    dm.getChip = function (content: any) {
        return command({
            method: 'getChip',
            methodId: 'U0DulpmR',
            params: content,
            to: this.to,
        });
    };

    dm.getDeviceId = function (content: any) {
        return command({
            method: 'getDeviceId',
            methodId: 'UrPw8XiI',
            params: content,
            to: this.to,
        });
    };

    dm.getFlashSize = function (content: any) {
        return command({
            method: 'getFlashSize',
            methodId: 'UYMQPKro',
            params: content,
            to: this.to,
        });
    };

    dm.getKey = function (content: any) {
        return command({
            method: 'getKey',
            methodId: 'UqocHFB9',
            params: content,
            to: this.to,
        });
    };

    dm.getProductBoard = function (content: any) {
        return command({
            method: 'getProductBoard',
            methodId: 'U3f3yn1k',
            params: content,
            to: this.to,
        });
    };

    dm.getProductCode = function (content: any) {
        return command({
            method: 'getProductCode',
            methodId: 'UWbSjL4n',
            params: content,
            to: this.to,
        });
    };

    dm.getProductDevice = function (content: any) {
        return command({
            method: 'getProductDevice',
            methodId: 'UXOZIa2z',
            params: content,
            to: this.to,
        });
    };

    dm.getProductManufacturer = function (content: any) {
        return command({
            method: 'getProductManufacturer',
            methodId: 'UBx2n7uW',
            params: content,
            to: this.to,
        });
    };

    dm.getSerialNo = function (content: any) {
        return command({
            method: 'getSerialNo',
            methodId: 'UVg0TttE',
            params: content,
            to: this.to,
        });
    };

    dm.getToken = function (content: any) {
        return command({
            method: 'getToken',
            methodId: 'F4jCFSbM',
            params: content,
            to: this.to,
        });
    };

    // ==================================================
    /**
     * OTA
     *
     * @see https://ox8zrpl57d.feishu.cn/wiki/ROUmwFYNLi01j4kANUTclkiAnOf
     */
    // ==================================================
    dm.otaUpgrade = function (content: any) {
        return command({
            method: 'otaUpgrade',
            methodId: 'FQwlmVLK',
            params: content,
            to: this.to,
        });
    };

    dm.getCurrentFirmwareVersion = function (content: any) {
        return command({
            method: 'getCurrentFirmwareVersion',
            methodId: 'FaeDK3Ul',
            params: content,
            to: this.to,
        });
    };

    dm.getFirmwareDownloadProgress = function (content: any) {
        return command({
            method: 'getFirmwareDownloadProgress',
            methodId: 'FORT9GK5',
            params: content,
            to: this.to,
        });
    };

    dm.getOtaUpgradeStatus = function (content: any) {
        return command({
            method: 'getOtaUpgradeStatus',
            methodId: 'FSNrzxzm',
            params: content,
            to: this.to,
        });
    };

    dm.getBatteryStatus = function (content: any) {
        return command({
            method: 'getBatteryStatus',
            methodId: 'FbymobNP',
            params: content,
            to: this.to,
        });
    };

    dm.getDownloadSpaceStatus = function (content: any) {
        return command({
            method: 'getDownloadSpaceStatus',
            methodId: 'FRUuNcOw',
            params: content,
            to: this.to,
        });
    };

    dm.getFirmwareDownloadedStatus = function (content: any) {
        return command({
            method: 'getFirmwareDownloadedStatus',
            methodId: 'F962iY7U',
            params: content,
            to: this.to,
        });
    };

    dm.getFirmwareCheckStatus = function (content: any) {
        return command({
            method: 'getFirmwareCheckStatus',
            methodId: 'F6WiODeC',
            params: content,
            to: this.to,
        });
    };

    dm.otaReceive = function (content: any) {
        return command({
            method: 'otaReceive',
            methodId: 'F7TwtsDA',
            params: content,
            to: this.to,
        });
    };
};
